//
//  MYBaseNavViewController.swift
//  MYWeibo
//
//  Created by Magic Yu on 16/4/13.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit

class MYBaseNavViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置导航条背景图片
        //navigationBar.setBackgroundImage(<#T##backgroundImage: UIImage?##UIImage?#>, forBarMetrics: <#T##UIBarMetrics#>)
        self.interactivePopGestureRecognizer?.delegate = self
    }

    //重写了不super
    //根视图控制器无法添加   -> UINavigationController(rootViewController:XXX)
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //print(childViewControllers.count)
        //设置viewController 的navgitem.left
        
        //获取控制器的title
        
        if childViewControllers.count > 0 {
            
            var backTitle = "返回"
            if childViewControllers.count == 1 {
                backTitle = childViewControllers.first?.title ?? ""
            }
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: "back")
            viewController.hidesBottomBarWhenPushed = true
            //设置颜色
            viewController.view.backgroundColor = UIColor.whiteColor()
        }
        super.pushViewController(viewController, animated: animated)
        //print(childViewControllers.count)
    }
    
    
    @objc private func back() {
        self.popViewControllerAnimated(true)
    }

    //手势将要开始的协议方法
    //协议方法返回值是 bool值 如果返回ture  就可以正常工作,如果返回false 手势就会失效
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        //print("哈哈哈")
        return childViewControllers.count != 1
        //        if childViewControllers.count == 1 {
        //            return false
        //        }
        //        return true
    }


}
